import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'

const modules = import.meta.glob('@/pages/**/*.vue')
const autoRoutes = []
for (let path in modules) {
  const fileName = path.split('/')?.pop()?.replace('.vue', '')
  autoRoutes.push({
    path: '/' + fileName,
    component: modules[path],
    name: fileName?.toLocaleUpperCase()
  })
}
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home'
  },
  ...autoRoutes,
  {
    path: '/*',
    redirect: '/404'
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory(), //createWebHistory(),
  scrollBehavior(_to, _from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
